package model

import (
	"openapi.itspire.cn/database"
	"openapi.itspire.cn/utils"
)

type Comment struct {
	Id         	  uint64 `json:"id"`
	Tid        	  uint64 `json:"tid"`
	Pid           uint64 `json:"pid"`
	ArticleId     uint64 `json:"articleId"`
	UserId        uint64 `json:"userId"`
	Nickname   	  string `json:"nickname"`
	Content       string `json:"content"`
	Ip       	  uint64 `json:"ip"`
	Status        uint64 `json:"status"`
	CreateTime    uint64 `json:"createTime"`
	UpdateTime    uint64 `json:"updateTime"`
	AuditTime     uint64 `json:"auditTime"`
	ReplyId       uint64 `json:"replyId" gorm:"-"`
	ReplyNickname string `json:"replyNickname" gorm:"-"`
	ContactType   uint8  `json:"contactType" gorm:"-"`
	Contact       string `json:"contact" gorm:"-"`
	Title         string `json:"title" gorm:"-"`
	Avatar	      string `json:"avatar" gorm:"-"`
}

func (c Comment) GetTableName() string {
	return database.Ins.NewScope(c).TableName()
}

func (c Comment) GetFormatComment(comment []Comment) []map[string]interface{} {
	commentMaps := make([]map[string]interface{}, 0)
	childrenMaps := make([]map[string]interface{}, 0)
	for _, v := range comment {
		maps := utils.StructToMap(v, "json")
		if v.Pid == 0 {
			commentMaps = append(commentMaps, maps)
		} else {
			childrenMaps = append(childrenMaps, maps)
		}
	}

	for _, v := range commentMaps {
		children := make([]map[string]interface{}, 0)
		for _, vv := range childrenMaps {
			if v["tid"] == vv["tid"] {
				children = append(children, vv)
				v["children"] = children
			}
		}
	}

	return commentMaps
}

